package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyds.common.MyJSON;
import com.yyds.mapper.TravelWayMapper;
import com.yyds.pojo.TravelWay;
import com.yyds.service.TravelWayService;
import com.yyds.vo.WayTravelWayEchartPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("travelWayService")
public class TravelWayServiceImpl implements TravelWayService {
    @Autowired
    private TravelWayMapper travelWayMapper;//DI自动注入

    @Override
    public MyJSON<TravelWay> selectAllTravelWayService(Map<String, Object> params) {
        //mybatis分页插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<TravelWay> travelWayList =  travelWayMapper.selectByParams(params);

        //mybatis的分页数据结构
        PageInfo<TravelWay> travelWayPageInfo = new PageInfo<>(travelWayList);
        //把mybatis的分页数据结构中的数据封装到layui的分页数据结构中
        MyJSON<TravelWay> travelWayMyJSON = new MyJSON<>();
        travelWayMyJSON.setCode(0);
        travelWayMyJSON.setMsg("出行方式分页列表数据");
        travelWayMyJSON.setCount( travelWayPageInfo.getTotal());  // 总记录数
        travelWayMyJSON.setData(travelWayPageInfo.getList());  // 分页列表数据

        return travelWayMyJSON;

    }

    @Override
    public int deleteTravelWayService(Integer way_tid) {
            return travelWayMapper.deleteTravelWayMapper(way_tid);

    }

    @Override
    public List<WayTravelWayEchartPieVo> queryWayTravelWayService() {
        return travelWayMapper.queryWayTravelWayMapper();
    }


    @Override
    public int addTravelWayService(TravelWay travelWay) {
        return travelWayMapper.addTravelWayMapper(travelWay);
    }

    @Override
    public int updateTravelWayService(TravelWay travelWay) {
        return travelWayMapper.updateTravelWayMapper(travelWay);
    }

    @Override
    public Integer deleteAllTravelWayService(String str) {
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return travelWayMapper.deleteAllTravelWayMapper(list);
    }
}
